<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\WxMiniprogramConfig;
use App\Services\WxMiniprogramService;
use Illuminate\Http\Request;

class WxMiniprogramController extends Controller
{
    private function returnFailUc($code, $msg)
    {
        return json_encode(['code' => $code, 'msg' => $msg]);
    }

    /**
     * UC-锦帆建站-微信营销组件-小程序-配置获取scheme url
     *
     * @param Request $request
     * @return void
     */
    public function ucGenerateScheme(Request $request)
    {
        $origin_id = $request->input('user_name');
        $path = $request->input('path');
        $query = $request->input('query');

        $config = WxMiniprogramConfig::where('origin_id', $origin_id)->first();
        if (!$config) {
            return $this->returnFailUc(9999, 'no config data');
        }

        $scheme = WxMiniprogramService::getInstance()->generateUrlScheme($config->appid, $path, $query);
        if (!$scheme) {
            return $this->returnFailUc(9999, 'generate error');
        }

        return json_encode([
            'code' => 0,
            'url_scheme' => $scheme,
        ]);
    }

    /**
     * 百度-营销通-获取scheme url
     *
     * @param Request $request
     * @return void
     */
    public function baiduGenerateScheme(Request $request)
    {
        $origin_id = $request->input('originId');
        $path = $request->input('path');
        $query = $request->input('query');

        $config = WxMiniprogramConfig::where('origin_id', $origin_id)->first();
        if (!$config) {
            return $this->returnFailUc(9999, 'no config data');
        }

        $scheme = WxMiniprogramService::getInstance()->generateUrlScheme($config->appid, $path, $query);
        if (!$scheme) {
            return $this->returnFailUc(9999, 'generate error');
        }

        return json_encode([
            'code'   => 0,
            'msg'    => 'success',
            'result' => [
                'scheme' => $scheme,
            ],
        ]);
    }

    public function generateUrlScheme(Request $request)
    {
        $appid = $request->input('appid');
        $path = $request->input('path');
        $query = $request->input('query');

        $scheme = WxMiniprogramService::getInstance()->generateUrlScheme($appid, $path, $query);
        if (!$scheme) {
            return yqerr(6046);
        }

        $sdk_link = config('app.url') . "/wx/miniprogram/jump?scheme=" . base64_encode($scheme);
        return yqok([
            'url_scheme' => $scheme,
            'sdk_link' => $sdk_link,
        ]);
    }

    public function generateUrllink(Request $request)
    {
        $appid = $request->input('appid');
        $path = $request->input('path');
        $query = $request->input('query');

        $link = WxMiniprogramService::getInstance()->generateUrllink($appid, $path, $query);
        if (!$link) {
            return yqerr(6046);
        }

        return yqok([
            'url_link' => $link,
        ]);
    }
}
